package com.rdzn.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rdzn.mall.base.util.DateTimeUtils;
import com.rdzn.mall.center.entity.UmsFootprint;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.service.IPmsProductService;
import com.rdzn.mall.center.service.IUmsFootprintService;
import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.portal.domain.Footprint;
import com.rdzn.mall.portal.domain.FootprintProduct;
import com.rdzn.mall.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
* @Description: ums_footprint
* @Author: jeecg-boot
* @Date:   2020-10-13
* @Version: V1.0
*/
@Api(tags="ums_footprint")
@RestController
@RequestMapping("/footprint")
@Slf4j
public class MUmsFootprintController extends JeecgController<UmsFootprint, IUmsFootprintService> {
   @Autowired
   private IUmsFootprintService umsFootprintService;
   @Autowired
   private IUserService userService;
   @Autowired
   private IPmsProductService productService;

   /**
    * 分页列表查询
    *
    * @param umsFootprint
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "ums_footprint-分页列表查询")
   @ApiOperation(value="ums_footprint-分页列表查询", notes="ums_footprint-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(UmsFootprint umsFootprint,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       UmsMember member = userService.getCurrentMember();
       if (member == null){
           return Result.error("未登录");
       }
       umsFootprint.setUserId(member.getId());
       QueryWrapper<UmsFootprint> queryWrapper = QueryGenerator.initQueryWrapper(umsFootprint, req.getParameterMap());
       Page<UmsFootprint> page = new Page<UmsFootprint>(pageNo, pageSize);
       IPage<UmsFootprint> pageList = umsFootprintService.page(page, queryWrapper);
       IPage<Footprint> data = pageList.convert(umsFootprint1 -> {
           Footprint footprint = new Footprint();
           FootprintProduct product = new FootprintProduct();
           product.setFootprint(umsFootprint1);
           product.setProduct(productService.getById(umsFootprint1.getProductId()));
           footprint.setUpdateTime(umsFootprint1.getUpdateTime());
           footprint.getProductList().add(product);
           return footprint;
       });
       List<Footprint> footprints = data.getRecords();
       if (CollectionUtils.isNotEmpty(footprints)){
           Map<String, Footprint> map = new HashMap<>();
           footprints.forEach(footprint -> {
               String time = DateTimeUtils.formatDate(footprint.getUpdateTime(), null);
               Footprint temp = map.get(time);

               if (temp != null && DateTimeUtils.formatDate(temp.getUpdateTime(), null).equals(time)){
                   temp.getProductList().addAll(footprint.getProductList());
               }else {
                   temp = new Footprint();
                   temp.setUpdateTime(footprint.getUpdateTime());
                   temp.getProductList().addAll(footprint.getProductList());
                   map.put(time, temp);
               }
           });

           data.setRecords(new ArrayList<>(map.values()).stream().sorted(Comparator.comparing(Footprint::getUpdateTime).reversed()).collect(Collectors.toList()));
       }
       return Result.ok(data);
   }

   /**
    *   添加
    *
    * @param umsFootprint
    * @return
    */
   @AutoLog(value = "ums_footprint-添加")
   @ApiOperation(value="ums_footprint-添加", notes="ums_footprint-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody UmsFootprint umsFootprint) {
       umsFootprintService.save(umsFootprint);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param umsFootprint
    * @return
    */
   @AutoLog(value = "ums_footprint-编辑")
   @ApiOperation(value="ums_footprint-编辑", notes="ums_footprint-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody UmsFootprint umsFootprint) {
       umsFootprintService.updateById(umsFootprint);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "ums_footprint-通过id删除")
   @ApiOperation(value="ums_footprint-通过id删除", notes="ums_footprint-通过id删除")
   @GetMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       umsFootprintService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "ums_footprint-批量删除")
   @ApiOperation(value="ums_footprint-批量删除", notes="ums_footprint-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.umsFootprintService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "ums_footprint-通过id查询")
   @ApiOperation(value="ums_footprint-通过id查询", notes="ums_footprint-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       UmsFootprint umsFootprint = umsFootprintService.getById(id);
       if(umsFootprint==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(umsFootprint);
   }

    public static void main(String[] args) {
        Date date = new Date();
        Date date1 = new Date();
        System.out.println(date.toString()+ " -- "+date1.toString());
        System.out.println(date.equals(date1));
        System.out.println(date == date1);
    }

}
